//package com.itheima.tlias08_14.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.itheima.tlias08_14.dto.LoginDTO;
//import com.itheima.tlias08_14.mapper.EmpMapper;
//import com.itheima.tlias08_14.utils.JwtUtil;
//import com.itheima.tlias08_14.utils.ResultUtil;
//import jakarta.annotation.Resource;
//import jakarta.servlet.*;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.Objects;
//
//@Component
//@Slf4j
//public class LoginFilter implements Filter {
//    @Autowired
//    private EmpMapper empMapper;
//
//    @Override
//    //初始化方法, 只调用一次
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//        log.info("过滤器初始化了");
//    }
//    @Resource
//    EmpMapper mapper;
//
//    @Override
//    //拦截到请求之后调用, 调用多次
//    public void doFilter(ServletRequest servletRequest,
//                         ServletResponse servletResponse,
//                         FilterChain filterChain)
//            throws IOException, ServletException {
//        log.info("过滤器开始执行了");
//        //将ServletRequest 强转为HttpServletRequest
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        //将ServletResponse 强转为HttpServletResponse
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        //获取请求体中的uri
//        String rqsUri = request.getRequestURI();
//        //判断uri是否是登录的uri("login")
//        if(rqsUri.contains("login")){
//            log.info("登录请求");
//            //放行继续执行Controller
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//        //如果不包括含登录的uri，则判断是否登录
//        //获取请求头中的token
//        String token = request.getHeader("token");
//        //再判断token是否为空
//        if(Objects.isNull(token)){
//            log.info("token为空");
//            printErroMessage(response,"token为空");
//            return;
//        }
//
//        try{
//            //将获取的token解析
//            JSONObject decode = JwtUtil.decode(token);
//            //判断被解析出来的token对象中存放的信息是否有效
//            LoginDTO user = empMapper.selectByUsername(decode.getString("username"));
//
//            if(user != null && !user.getPassword().equals(decode.getString("password"))){
//                printErroMessage(response,"token中解析密码错误");
//                return;
//            }
//
//        } catch(RuntimeException e){
//            log.info("token过期了");
//            printErroMessage(response,"token过期了");
//            return;
//
//        }
//        //如果请求体中的uri不为login且存在token，则继续执行controller
//        //doFilter 将请求传递给Controller层
//        filterChain.doFilter(servletRequest,servletResponse);
//        log.info("过滤器执行结束了");
//    }
//
//    private static void printErroMessage(HttpServletResponse response,String message) throws IOException {
//        //设置servlet输出编码utf-8
//        response.setContentType("application/json;charset=utf-8");
//        response.setCharacterEncoding("utf-8");
//
//        //修改响应状态码
//        //response.setStatus(401);
//        //response.setStatus(HttpStatus.SC_UNAUTHORIZED);
//        response.setStatus(HttpStatus.UNAUTHORIZED.value());
//        //如果为空，则返回错误信息
//        PrintWriter writer = response.getWriter();
//        ResultUtil failure = ResultUtil.failure(message);
//        //将错误信息转为json形式返回给前端
//        writer.write(JSONObject.toJSONString(failure));
//    }
//
//    @Override
//    //当我们关闭服务器的时候，它会自动的调用销毁方法destroy
//    //销毁方法, 只调用一次
//    public void destroy() {
//        Filter.super.destroy();
//        log.info("过滤器销毁了");
//    }
//}
